let CouponId;
layui.config({
    version:'1510164124181'
}).use(['form','laydate', 'layer', 'element','upload'],function(){
    form = layui.form;
    layer = layui.layer;
    laydate = layui.laydate;//日期
    upload = layui.upload;//上传
    element = layui.element; //元素操作
    //日期时间范围
    laydate.render({
        elem: '#times', type: 'datetime', range: true,format:'yyyy-MM-dd'
    });
    CouponId=GetQueryString("CouponId");
    TypelistByTypeId(3);
    if(!isNotNull(CouponId)){
        CouponId=0;
    }else{
        SelectInfoById();
    }

});
function SelectInfoById(){
    $.ajax({
        url: httpurl+"/findCouponListId",
        cache: false,
        headers: {
            "Access-Control-Allow-Origin": "*",
            "Access-Control-Allow-Methods": "*",
            "Access-Control-Allow-Headers": "Authorization",
            "Authorization": GetTokenInfo()
        },
        contentType : 'application/json',
        data: {
            "id":CouponId
        },
        dataType: "json",
        success: function(data) {
            if(data.rtnCode==200){
                let dataInfo=data.data;
                let b = isNotNull(dataInfo.typeList == null) ? "" : dataInfo.typeList.id;
                $("#types option[value="+b+"]").attr("selected","selected");
                $("#name").val(dataInfo.name);
                $("#val").val(dataInfo.val);
                $("#GlodNum").val(dataInfo.glodnum);
                $("#Concent").val(dataInfo.content);
                $("#MoreMoney").val(dataInfo.fullmoney);
                $("#CardDay").val(dataInfo.days);
                $("#CardNum").val(dataInfo.num);
                $("#times").val(dataInfo.starttime+" - "+dataInfo.endtime);
                $("#onlynum").val(dataInfo.onlynum);
                let isdispaly = dataInfo.isdispaly;
                $("input:radio[value='"+isdispaly+"']").attr("checked", "checked");//活动范围
            }
        }
    });
}

function SubmitInfo(){
    let typeid = $("#types option:selected").val();
    if(typeid==0){layer.alert("请选择优惠券类型!");return false;}
    var name=$("#name").val();
    if(name==""){layer.alert("请输入优惠券名称!");return false;}
    var val=$("#val").val();
    if(val==""){layer.alert("请输入优惠券面值!");return false;}
    var GlodNum=$("#GlodNum").val();
    if(GlodNum==""){layer.alert("请输入需要金币数量!");return false;}
    var MoreMoney=$("#MoreMoney").val();
    if(MoreMoney==""){layer.alert("请输入最少满足金额!");return false;}
    var Concent=$("#Concent").val();
    if(Concent==""){layer.alert("请输入优惠券内容!");return false;}

    let times = $("#times").val();//下单时间
    if(times==""){layer.alert("开始时间-结束时间!");return false;}
    let StartDate = times.split(" - ")[0];
    let EndDate = times.split(" - ")[1];
    var CardDay=$("#CardDay").val();
    var CardNum=$("#CardNum").val();
    var onlynum=$("#onlynum").val();
    if(CardDay==""){layer.alert("请输入天数!");return false;}
    if(CardNum==""){layer.alert("请输入数量!");return false;}
    if(onlynum==""){layer.alert("请输入每人限领数量!");return false;}

    let isdispaly = $('input[name="isdispaly"]:checked ').val();//获取选中的值
    top.layer.confirm('是否确认提交？',{icon: 3,title:'提示信息'},function(index){
        var index = top.layer.msg('提交中，请稍候',{icon: 16,time:false,shade:0.8});
        var HttpAjax="/updateCouponById";
        if(CouponId==0){
            HttpAjax="/insertCoupon";
        }
        setTimeout(function(){
            $.ajax({
                type:"POST",
                url: httpurl+HttpAjax,
                cache: false,
                headers: {
                    "Access-Control-Allow-Origin": "*",
                    "Access-Control-Allow-Methods": "*",
                    "Access-Control-Allow-Headers": "Authorization",
                    "Authorization": GetTokenInfo()
                },
                contentType : 'application/json',
                data: JSON.stringify({
                    "id":CouponId,
                    "name":name,
                    "val":val,
                    "glodnum":GlodNum,
                    "content":Concent,
                    "startTimes":StartDate,
                    "fullmoney":MoreMoney,
                    "days":CardDay,
                    "num":CardNum,
                    "onlynum":onlynum,
                    "endTimes":EndDate,
                    "tid":typeid,
                    "isdispaly": isdispaly

                }),
                success: function(data) {
                    layer.closeAll();
                    // console.log(JSON.stringify(data));
                    top.layer.msg(data.msg);
                    re();
                }
            });
        },1000);
    });
}
function re() {
    window.location.href="CouponList.html";
}
